function Form (id) {
	this.id = id;
	this.formObj = document.getElementById(id);
	
	//不需要校验
	this.noValidate = (this.formObj.getAttribute("noValidate") != null);
	//是弹出式提示校验失败
	this.alertValidMsg = (this.formObj.getAttribute("alertValidMsg") != null);
	//不需要快速定位
	this.noEasyFocus = (this.formObj.getAttribute("noEasyFocus") != null);
	//该form是一个readonly的form
	this.readOnly = (this.formObj.getAttribute("readOnly") != null)
	
	
	
	var oThis = this;
//	if (this.readOnly) {
//		var elements = this.formObj.elements;
//		if (elements != null) {
//			for (var i = 0; i < elements.length; i++) {
//				//if (elements[i].tagName == "SELECT") elements[i].value = "";
//				if (elements[i].tagName == "TEXTAREA" && elements[i].readOnly) {
//					elements[i].style.color = "inactiveborder";
//				}
//				if (elements[i].tagName == "INPUT" && elements[i].readOnly) {
//					if (elements[i].type == "text") {
//						elements[i].style.color = "inactiveborder";
//					}
//				}
//			}
//		}
//	}
	
	
	//需要校验，那么在onsubmit方法里捕获
	if (!this.noValidate) {
		var f = function () {return oThis.validForm();}
		if (document.all) {
			this.formObj.attachEvent("onsubmit", f);
		} else {
			this.formObj.addEventListener("submit", f, false);
		}
	}
	
	//如果是dialog window中
//	if (window.dialogArguments) {
//		alert("xx");
//		if (this.formObj.target == null || this.formObj.target == "" || !this.formObj.target) {
//			this.formObj.target = "selfDialog";	
//			alert("yy");
//		}
//		
//		if (window.name == null || window.name == "" || !window.name) {
//			window.name = "selfDialog";
//			alert("zz");
//		}
//	}
	
	//为某一个增加校验onchange事件
	this.addValidOnChangeEvent = function (objItem) {
		var f = function () {oThis.validField(objItem);}
		if (document.all) {
			objItem.attachEvent("onchange", f);
		} else {
			objItem.addEventListener("change", f, false);
		}
	}
	
	//为某一个item增加easy focus事件
	this.addEasyFocusEvent = function (objItem) {
		if (objItem.getAttribute("focusOrder") != null) {
			var f = function (event) {oThis.easyNextFocus(event, objItem);}
			if (document.all) {
				objItem.attachEvent("onkeydown", f);
			} else {
				objItem.addEventListener("keydown", f, false);
			}
		}
	}
	
	//为某一个item增加url校验事件
	this.addAJAXValidEvent = function (objItem) {
		//只允许text输入框和select框作ajax校验
		if (objItem.type != "text" && objItem.tagName != "SELECT") return;
		
		//参数
		var url = objItem.getAttribute("ajaxValidURL");
		var method = objItem.getAttribute("ajaxValidMethod");
		
		//url必须有效
		if (isValid(url)) {
			var f = function () {oThis.validItemByAJAX(objItem, url, method);}
			if (document.all) {
				objItem.attachEvent("onchange", f);
			} else {
				objItem.addEventListener("change", f, false);
			}
		}
	}
	
	//为某一个item增加提示输入事件
	this.addAJAXHelpInputEvent = function (objItem) {
		//只允许text输入框
		if (objItem.type != "text") return;
		
		//参数
		var url = objItem.getAttribute("ajaxHelpInputURL");
		var method = objItem.getAttribute("ajaxHelpInputMethod");
		
		//url必须有效
		if (isValid(url)) {
			var f = function (event) {oThis.helpInputByAJAX(event, objItem, url, method);}
			if (document.all) {
				objItem.attachEvent("onkeydown", f);
			} else {
				objItem.addEventListener("keydown", f, false);
			}
		}
	}
	
	//为某一个item自动输入事件
	this.addAJAXAutoInputEvent = function (objItem) {
		//只允许text输入框和select框作ajax校验
		if (objItem.type != "text" && objItem.tagName != "SELECT") return;
		
		//参数
		var url = objItem.getAttribute("ajaxAutoInputURL");
		var method = objItem.getAttribute("ajaxAutoInputMethod");
		var alo = objItem.getAttribute("ajaxAutoInputBindingList");
		
		//url必须有效
		if (isValid(url)) {
			var f = function () {oThis.autoInputByAJAX(objItem, url, method, alo);}
			if (document.all) {
				objItem.attachEvent("onchange", f);
			} else {
				objItem.addEventListener("change", f, false);
			}
		}
	}
	
	//开始遍历每一个element为其增加事件
	for (var i = 0; i < this.formObj.length; i++) {
		var objItem = this.formObj.elements[i];
		//input可以进行自动完成，帮助完成，easy focus，url校验，
		if (objItem.tagName == 'INPUT') {
			this.addAJAXAutoInputEvent(objItem);
			this.addAJAXHelpInputEvent(objItem);
			this.addAJAXValidEvent(objItem);
			this.addEasyFocusEvent(objItem);
		//select可以进行自动完成，easy focus，和url校验
		} else if (objItem.tagName == 'SELECT') {
			this.addAJAXAutoInputEvent(objItem);
			this.addAJAXValidEvent(objItem);
			this.addEasyFocusEvent(objItem);
			
		//text area进行easy focus
		} else if (objItem.tagName == 'TEXTAREA') {
			this.addEasyFocusEvent(objItem);
		}
		
		
		//添加校验的一些事件
		if (!this.noValidate) {
			//增加非空的标志
			if (objItem.getAttribute("notNull") != null) {
				objItem.insertAdjacentHTML("afterEnd", "<font color=\"red\">*</font>");
			}
			//检查是否是在change value的时候校验
			if (objItem.getAttribute("validOnChange") != null) {
				this.addValidOnChangeEvent(objItem);
			}
		}
	}	
}

//检查单个的field
Form.prototype.validField = function (objItem) {
	if (objItem.getAttribute("noValidate") != null) return true;
	if (objItem.disabled) return true;
	
	var msg = this.validItem(objItem);
	//校验不通过
	if (isValid(msg)) {
		this.showValidMsg(objItem, msg);
		return false;
	//校验通过
	} else {
		//如果是通过msg显示出来的，需要清除msg
		this.showValidMsg(objItem, "");
		return true;
	}
}

//检查form的方法
Form.prototype.validForm = function () {
	var success = true;
	var alertMsg = "";
	var firstField = null;

	//循环检查每一个需要检验的item
	for (var i = 0; i < this.formObj.length; i++) {
		var objItem = this.formObj.elements[i];
		if (objItem.disabled == true) continue;
		if (objItem.getAttribute("noValidate") != null) continue;
		
		var msg = this.validItem(objItem);
		//校验不通过
		if (isValid(msg)) {
			success = false;
			//如果需要alert，那么记录下来
			if (this.alertValidMsg) alertMsg += msg + "\r\n";
			//否则在field后的span中显示
			else this.showValidMsg(objItem, msg);
			
			//记录第一个field
			if (firstField == null) firstField = objItem;
		//校验通过
		} else {
			//如果是通过msg显示出来的，需要清除msg
			this.showValidMsg(objItem, "");
		}
	}
	//校验不成功，并且需要alert出来
	if (!success && this.alertValidMsg) alert(alertMsg);
	//将焦点定位到第一个field
	if (!success && firstField != null) firstField.focus();
	
	return success;
}

//重置form的方法
Form.prototype.resetForm = function () {
	var elements = this.formObj.elements;
	if (elements != null) {
		for (var i = 0; i < elements.length; i++) {
			if (elements[i].getAttribute("noReset") != null) continue;
			if (elements[i].tagName == "SELECT") elements[i].value = "";
			if (elements[i].tagName == "TEXTAREA") elements[i].innerText = "";
			if (elements[i].tagName == "INPUT") {
				if (elements[i].type == "text" || elements[i].type == "password" || elements[i].type == "hidden") elements[i].value = "";
				if (elements[i].type == "checkbox" || elements[i].type == "radio") elements[i].checked = false;
			}
		}
	}
}

//校验某一个element item, 返回空字符串表示校验通过，否则返回提示的msg
Form.prototype.validItem = function (objItem) {
	var resultMsg = "";
	if (objItem.getAttribute("notNull") != null) {
		if (!isValid(objItem.value)) {
			resultMsg += ((objItem.getAttribute("caption"))?objItem.getAttribute("caption"):"") + "不能为空 ";
		}
	}
	if (objItem.getAttribute("isNumber") != null) {
		if (isValid(objItem.value) && !isNumber(objItem.value)) {
			resultMsg += ((objItem.getAttribute("caption"))?objItem.getAttribute("caption"):"") + "必须是数字 ";
		}
	}
	if (objItem.getAttribute("isFilterNumber") != null) {
		if (isValid(objItem.value) && !isFilterNumber(objItem.value)) {
			resultMsg += ((objItem.getAttribute("caption"))?objItem.getAttribute("caption"):"") + "必须是数字 ";
		}
	}
	if (objItem.getAttribute("isPositiveInt") != null) {
		if (isValid(objItem.value) && !isPositiveInt(objItem.value)) {
			resultMsg += ((objItem.getAttribute("caption"))?objItem.getAttribute("caption"):"") + "必须是正整数 ";
		}
	}
	if (objItem.getAttribute("isFilterPositiveInt") != null) {
		if (isValid(objItem.value) && !isFilterPositiveInt(objItem.value)) {
			resultMsg += ((objItem.getAttribute("caption"))?objItem.getAttribute("caption"):"") + "必须是正整数 ";
		}
	}
	if (objItem.getAttribute("equalLength") != null) {
		if (getUnicodeLength(objItem.value) != parseInt(objItem.getAttribute("equalLength"))) {
			resultMsg += ((objItem.getAttribute("caption"))?objItem.getAttribute("caption"):"") + "长度必须是" + objItem.getAttribute("equalLength") + " ";
		}
	}
	if (objItem.getAttribute("minLength") != null) {
		if (getUnicodeLength(objItem.value) < parseInt(objItem.getAttribute("minLength"))) {
			resultMsg += ((objItem.getAttribute("caption"))?objItem.getAttribute("caption"):"") + "长度至少必须是" + objItem.getAttribute("minLength") + " ";
		}
	}
	if (objItem.getAttribute("maxLength") != null) {
		if (getUnicodeLength(objItem.value) > parseInt(objItem.getAttribute("maxLength"))) {
			resultMsg += ((objItem.getAttribute("caption"))?objItem.getAttribute("caption"):"") + "长度不能超过" + objItem.getAttribute("maxLength") + " ";
		}
	}
	if (objItem.getAttribute("min") != null && !isNaN(objItem.value)) {
		if (parseInt(objItem.value) < parseInt(objItem.getAttribute("min"))) {
			resultMsg += ((objItem.getAttribute("caption"))?objItem.getAttribute("caption"):"") + "数字大小必须超过" + objItem.getAttribute("min") + " ";
		}
	}
	if (objItem.getAttribute("max") != null && !isNaN(objItem.value)) {
		if (parseInt(objItem.value) > parseInt(objItem.getAttribute("max"))) {
			resultMsg += ((objItem.getAttribute("caption"))?objItem.getAttribute("caption"):"") + "数字大小不能超过" + objItem.getAttribute("max") + " ";
		}
	}
    //regexp
	if (objItem.getAttribute("regexp") != null) {
		if (!checkRegexp(objItem.value, objItem.getAttribute("regexp"))) {
			resultMsg += ((objItem.getAttribute("caption"))?objItem.getAttribute("caption"):"") + "格式必须是" + objItem.getAttribute("regexp") + " ";
		}
	}
    //isFilterDate
	if (objItem.getAttribute("isFilterDate") != null) {
		if (isValid(objItem.value) && !isFilterDate(objItem.value)) {
			resultMsg += ((objItem.getAttribute("caption"))?objItem.getAttribute("caption"):"") + "必须是日期 ";
		}
	} 
	//isDate
	if (objItem.getAttribute("isDate") != null) {
		if (isValid(objItem.value) && !isDate(objItem.value)) {
			resultMsg += ((objItem.getAttribute("caption"))?objItem.getAttribute("caption"):"") + "必须是日期 ";
		}
	}
    //isEmail
	if (objItem.getAttribute("isEmail") != null) {
		if (isValid(objItem.value) && !isEmail(objItem.value)) {
			resultMsg = ((objItem.getAttribute("caption"))?objItem.getAttribute("caption"):"") + "必须是Email格式 ";
		}
	}
    //isURL
	if (objItem.getAttribute("isURL") != null) {
		if (isValid(objItem.value) && !isURL(objItem.value)) {
			resultMsg += ((objItem.getAttribute("caption"))?objItem.getAttribute("caption"):"") + "必须是网址 ";
		}
	}
    //isPhone
	if (objItem.getAttribute("isPhone") != null) {
		if (isValid(objItem.value) && !isPhone(objItem.value)) {
			resultMsg += ((objItem.getAttribute("caption"))?objItem.getAttribute("caption"):"") + "必须是电话号码 ";
		}
	}  
    //isWordChar
	if (objItem.getAttribute("isWordChar") != null) {
		if (isValid(objItem.value) && !isWordChar(objItem.value)) {
			resultMsg += ((objItem.getAttribute("caption"))?objItem.getAttribute("caption"):"") + "必须是有效字符 ";
		}
	}
	//custom validate
	if (objItem.getAttribute("customValidate") != null) {
		var f = objItem.getAttribute("customValidate");
		eval("var isF = (typeof window." + f + " == 'function');");
		if (isF) {
			try {
				eval("var r = window." + f + "(objItem, this.formObj);");
				if (isValid(r)) resultMsg += r;
			} catch (o) {
				alert(o);
			}
		}
	}
	
	//ajax validate
	if (objItem.getAttribute("ajaxValidResult") != null) {
		var r = objItem.getAttribute("ajaxValidResult");
		if (r == "false") {
			resultMsg += "业务校验不通过 ";
		}
	}
	
	return resultMsg;
}

//在Input后增加一个span来显示校验信息的方法
Form.prototype.showValidMsg = function (objItem, msg) {
	var spanId = ((objItem.id)?objItem.id:objItem.name) + "_spanvalidinfo";
	var oSpan = document.getElementById(spanId);
	if (!oSpan) {
		objItem.insertAdjacentHTML("afterEnd", "<span id=\"" + spanId + "\" class=\"validatemsg_span\"></span>");
		oSpan = document.getElementById(spanId);
	}
	oSpan.innerHTML = msg;
}



//利用ajax校验input方法
Form.prototype.validItemByAJAX = function (objItem, url, method) {
	//调用校验用的url
	var http = new HttpObject();
	var msg = null;
	if (method == "POST")
		msg = http.httpPostReturnString(url, objItem.name + "=" + objItem.value);
	else
		msg = http.httpGetReturnString(url + "&" + objItem.name + "=" + objItem.value);
	
	//不成功返回的msg非空，非OK，非true
	if (!(msg == '' || msg == 'OK' || msg == 'true')) {	
		//如果不是alert方式
		if (!this.alertValidMsg) 
			this.showValidMsg(objItem, msg);
		//是alert方式
		else 
			alert(msg);
		objItem.focus();
		objItem.setAttribute("ajaxValidResult", "false");
	} else {
		if (!this.alertValidMsg) 
			this.showValidMsg(objItem, "");
		objItem.setAttribute("ajaxValidResult", "true");
	}
}

//填完一个主键后，自动完成的方法
Form.prototype.autoInputByAJAX = function (objItem, url, method, alo) {
	//调用取值的url
	var http = new HttpObject();
	var ret = null;
	if (method == "POST")
		ret = http.httpPostReturnMaps(url, objItem.name + "=" + objItem.value);
	else
		ret = http.httpGetReturnMaps(url + "&" + objItem.name + "=" + objItem.value);
	if (ret.length == 0) return;
	

	//获取其他对象，如果参数中有alo，那么split之,没有就从ret的参数中取key
	var objsId = null;
	if (alo) {
		objsId = alo.split(",");
	} else {
		objsId = keyOfHash(ret[0]);
	}
	
	//根据objs的id来寻找对象，并且赋值
	for (var i = 0; objsId && i < objsId.length; i ++) {
		//搜寻field
		var obj = document.getElementById(objsId[i]);
		if (!obj) obj = this.formObj[objsId[i]];
		
		//找到了field对象
		if (obj) {
			///如果是普通text
			if (obj.tagName == "INPUT" && obj.type == 'text') {
				obj.value = ret[0][obj.name];
			}
		}
	}
	
}

//填完部分信息，用down键搜索可使用
Form.prototype.helpInputByAJAX = function (event, objItem, url, method) {
	var div = ((objItem.id)?objItem.id:objItem.name) + "_divvalidinfo";
	var oDiv = document.getElementById(div);
	
	//up键，关闭div，并且清除选项
	if (event.keyCode == 38 && oDiv) {
		var sDiv = new SelectableDivs(oDiv, false, true);
		sDiv.hidden();
		sDiv.clearItem();
		return;
	}
	
	//不是down键，返回
	if (event.keyCode != 40) return;
	//div不存在，创建
	if (!oDiv) {
		objItem.insertAdjacentHTML("afterEnd", "<div id=\"" + div + "\" class=\"selected_div\" style=\"height:50px;display:none;\" onselectstart=\"return false\"></div>");
		oDiv = document.getElementById(div);
	}
	var sDiv = new SelectableDivs(oDiv, false, true);
	
	
	
	//通过url取可选的值
	var http = new HttpObject();
	var ret = null;
	if (method == "POST")
		ret = http.httpPostReturnArray(url, objItem.name + "=" + objItem.value);
	else
		ret = http.httpGetReturnArray(url + "&" + objItem.name + "=" + objItem.value);
	sDiv.clearItem();
	for (var i = 0; i < ret.length; i ++) {
		sDiv.appendItem(ret[i]);
	}

	//定位层的xy
	var dTop = posLib.getBoundingClientBottom(objItem);
	var dLeft = posLib.getBoundingClientLeft(objItem);
	sDiv.show(dLeft, dTop);
	if (sDiv.getItems().length !== 0) {
		sDiv.setItemSelected(sDiv.getItem(0), true);
		sDiv.getItem(0).focus();
	}
	
	//选中某个帮助输入的值
	sDiv.onclick = function () {
		var its = sDiv.getSelectedItems();
		if (its && its[0]) {
			objItem.value = its[0].innerHTML;
			objItem.focus();
		}
		this.hidden();
		this.clearItem();
	}
	
	//某些key事件
	sDiv.onkeydown = function () {
		//esc
		if (event.keyCode == 27) {
			this.hidden();
			this.clearItem();
			objItem.focus();
		//回车，表示选中改值
		} else if (event.keyCode == 13) {
			var its = sDiv.getSelectedItems();
			if (its && its[0]) {
				objItem.value = its[0].innerHTML;
				objItem.focus();
			}
			this.hidden();
			this.clearItem();
		}
		event.cancelBubble = true;
    	event.returnValue = false;
	}
}

//在form中利用上下键移动光标
Form.prototype.easyNextFocus = function (event, inputObj) {
		//38 up
		//40 down
		
	if ((event.keyCode == 38 || event.keyCode == 40) && event.ctrlKey) {
		//获取当前input上的order
		var focusOrder = parseInt(inputObj.getAttribute("focusOrder"));
		if (focusOrder) {
			var targetObj = null;
				//遍历当前Input所在form下的所有元素
			for (var i = 0; i < this.formObj.length; i++) {
				var elementOrder = parseInt(this.formObj.elements[i].getAttribute("focusOrder"));
				//该元素如果有focusorder
				if (elementOrder != null) {
					elementOrder = parseInt(elementOrder);
					//上移
					if (event.keyCode == 38 && elementOrder < focusOrder) {
						if (targetObj == null) {
							targetObj = this.formObj.elements[i];
						} else {
							if (elementOrder >= parseInt(targetObj.getAttribute("focusOrder"))) {
								targetObj = this.formObj.elements[i];
							}
						}
					//下移
					} else {
						if (event.keyCode == 40 && elementOrder > focusOrder) {
							if (targetObj == null) {
								targetObj = this.formObj.elements[i];
							} else {
								if (elementOrder < parseInt(targetObj.getAttribute("focusOrder"))) {
									targetObj = this.formObj.elements[i];
								}
							}
						}
					}
				}
			}
			if (targetObj != null) {
				targetObj.focus();
			}
		}
		return false;
	//不是上下键
	} else {
		return true;
	}
}